package com.baosight.sczx.zm.cn.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.Mine;
import com.baosight.sczx.zm.cn.domain.ZMCNMcmodule;
/**
 * 煤样编码管理
 */
public class ServiceZMCN01 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "50");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		
		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCNMcmodule.query", new ZMCNMcmodule());
		String mzCode=InitialData.getInstance().getEiBlockByNo("cmes.cn.meizhiCode").getCellStr(0, "display");
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true,mzCode));
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
//		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cn.stationName", "value", "display", true,"D.10223"));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cn.mineType")); 
		outInfo.addBlock(Mine.getMineBlock("cmes.ch.mine"));
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cn.status", new String[] { "草拟", "生效" }, new String[] { "草拟", "生效" }, false));// 状态
		return outInfo;
	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo();
		ZMCNMcmodule _dom = new ZMCNMcmodule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		EiBlock block=Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true,UserSession.getInSessionProperty("miningCode").toString());
		int rowcount = eiBlock.getRowCount();
		int i = 0;		
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_MCMODULE"));//主键
			//给分站名置值
			String stationid=_dom.getFdMyStation();
			for(int j=0;j<block.getRowCount();j++){
				if(block.getCellStr(j, "orgCode").equals(stationid)){
					_dom.setFdMyStationName(block.getCellStr(j, "orgName"));
				}
			}
			this.dao.insert("ZMCNMcmodule.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 	
		
		
//		EiInfo outinfo = new EiInfo();
//		ZMCNMcmodule _dom = new ZMCNMcmodule();
//		EiBlock eiBlock = inInfo.getBlock("result");
//		eiBlock.setBlockMeta(_dom.eiMetadata);
//		int rowcount = eiBlock.getRowCount();
//
//		for (int i = 0; i < rowcount; i++) {
//			_dom.fromMap(eiBlock.getRow(i));
//			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_MCMODULE"));
//			this.dao.insert("ZMCNMcmodule.insert", _dom);
//		}
//		outinfo = this.query(inInfo);
//		outinfo.setStatus(1);
//		outinfo.setMsg("操作" + rowcount + "记录成功");
//		return outinfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCNMcmodule _dom = new ZMCNMcmodule();
		EiBlock eiBlock = inInfo.getBlock("result");
		EiBlock block=Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true,UserSession.getInSessionProperty("miningCode").toString());
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			//给分站名置值
			String stationid=_dom.getFdMyStation();
			for(int j=0;j<block.getRowCount();j++){
				if(block.getCellStr(j, "orgCode").equals(stationid)){
					_dom.setFdMyStationName(block.getCellStr(j, "orgName"));
				}
			}
			this.dao.update("ZMCNMcmodule.update",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	
		
//		EiInfo outInfo = new EiInfo();
//		ZMCNMcmodule _dom = new ZMCNMcmodule();
//		EiBlock eiBlock = inInfo.getBlock("result");
//		eiBlock.setBlockMeta(_dom.eiMetadata);
//		int rowcount = eiBlock.getRowCount();
//		for (int i = 0; i < rowcount; i++) {
//			_dom.fromMap(eiBlock.getRow(i));
//			this.dao.update("ZMCNMcmodule.update", _dom);
//		}
//		outInfo = this.query(inInfo);
//		outInfo.setStatus(1);
//		outInfo.setMsg("操作" + rowcount + "记录成功");
//		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNMcmodule _dom = new ZMCNMcmodule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCNMcmodule.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCNMcmodule _dom = new ZMCNMcmodule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			this.dao.update("ZMCNMcmodule.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	
	/**
	 * 校验名称编号不能重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午14:34:17 王开兵
	 * </p>
	 *
	 */

	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNMcmodule _dom = new ZMCNMcmodule();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		int i = 0;		
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			String fdMyCode = _dom.getFdMyCode();
			String fdMyStation = _dom.getFdMyStation();
			params.put("fdMyCode", fdMyCode);
			params.put("fdMyStation", fdMyStation);
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			List result = dao.query("ZMCNMcmodule.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				String msg = "站点 煤样编码重复！";
				inInfo.setMsg(msg);
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
} 